<template>
  <div>
    <van-nav-bar title="锦州自助洗车" :right-text="rightText" @click-right="onClickRight"/>
    <van-swipe class="my-swipe" :autoplay="3000" indicator-color="white">
      <van-swipe-item>
        <img src="../assets/banner.png" alt="" srcset="" style="width:100%">
      </van-swipe-item>
    </van-swipe>
    <van-grid :column-num="2">
      <van-grid-item class="wash-icon" v-for="(value,index) in textVale" :key="index" :icon="value.img" :text="value.text" @click="indexRcord(value.text)" />
    </van-grid>
  </div>
</template>
<script>
// '我的账户','在线充值','充值记录','洗车记录'
export default {
  data(){
    return{
      textVale:[
        {
          img:'manager',
          text:'我的账户'
        },
        {
          img:'gold-coin',
          text:'在线充值'
        },
        {
          img:'balance-list',
          text:'充值记录'
        },
        {
          img:'column',
          text:'洗车记录'
        }
      ],
      rightText: '',
      openId:localStorage.getItem('openId') ? localStorage.getItem('openId') : '',
    }
  },
  mounted(){
    if(localStorage.getItem('token')){
      this.rightText = '退出'
      let appId = 'wx29a89680a19e6747'
      let url = window.location.search;
      let start = window.location.search.indexOf("=");
      let end = window.location.search.indexOf("&");
      let code = url.substring(start + 1, end); // 截取路径中的code，如果没有就去微信授权，如果已经获取到了就直接传code给后台获取openId
      const local = window.location.href
      // const local = 'http://www.pigeon-bazaar.com'
      if (code == null || code ==='') {
        if(!localStorage.getItem('openId')){
          window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' + appId + '&redirect_uri=' + encodeURIComponent(local) + '&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect'
        }
      }else{
        if(!localStorage.getItem('openId')){
           this.$api.wxlogin({code:code}).then(resp=>{
            if(resp.code==0){
              this.openId = resp.openId
              localStorage.setItem('openId',resp.openId)
            }else{
              this.$toast(resp.msg)
            }
          })
        }
      }

    }else{
      this.rightText = '登录'
    }
  },
  methods: {
      // 获取用户信息
    getInfor(){
      this.$api.inforApi().then(resp=>{
        if(resp.code == 0){
          this.infoObj = resp.accountInfo
        }else{
          this.$toast(resp.msg)
        }
      })
    },
    onClickLeft() {
      this.$router.push({
        name:'index'
      })
    },
    onClickRight() {
      if(localStorage.getItem('token')){
        this.$api.logout().then(resp=>{
          if(resp.code == 0){
            localStorage.removeItem('openId')
            localStorage.removeItem('token')
            this.$router.push({
              name:'login'
            })
          }
        })
      }else{
        this.$router.push({
          name:'login'
        })
      }
    },
    indexRcord(item){
      let self = this
      if(!localStorage.getItem('token')){
         this.$router.push({
          name:'login'
        })
        return
      }
      if(item == '充值记录'){
        this.$router.push({
          name:'fillingRecord'
        })
      }else if(item == '洗车记录'){
        this.$router.push({
          name:'washRecord'
        })
      }else if(item == '在线充值'){
        this.$router.push({
          name:'online',
          query:{
            openId: self.openId
          }
        })
      }else{
        this.$router.push({
          name:'infor'
        })
      }
    }
  },
}
</script>
<style>
.wash-icon .van-grid-item__icon{
  color:#1989fa;
}
</style>
